/*
 * @lc app=leetcode.cn id=594 lang=typescript
 *
 * [594] 最长和谐子序列
 */

// @lc code=start
function findLHS(nums: number[]): number {
    const cache = new Map<number, number>();
    nums.forEach((i) => {
        cache.set(i, (cache.get(i) || 0) + 1);
    });
    let max = 0;
    for (const [key, value] of cache.entries()) {
        if (cache.has(key - 1)) {
            max = Math.max(max, value + cache.get(key - 1)!);
        }
        if (cache.has(key + 1)) {
            max = Math.max(max, value + cache.get(key + 1)!);
        }
    }
    return max;
}
// @lc code=end
